data_panel$wave <- data_panel$wave - 1
data_cross$wave <- data_cross$wave - 1


#code republican, democrat, independent 
data_panel$pid <- NA
data_panel$pid[data_panel$pid_mod < 4] <- 0 #Democrat
data_panel$pid[data_panel$pid_mod == 5 | data_panel$pid_mod == 6 | data_panel$pid_mod == 7] <- 1 #Republican
data_panel$pid[data_panel$pid_mod == 4 | data_panel$pid_mod == 8] <- NA #Independent or Other


data_cross$pid <- NA
data_cross$pid[data_cross$pid7 < 4] <- 0 #Democrat
data_cross$pid[data_cross$pid7 == 5 | data_cross$pid7 == 6 | data_cross$pid7 == 7] <- 1 #Republican
data_cross$pid[data_cross$pid7 == 4 | data_cross$pid7 == 8] <- NA #Independent or Other

#white vs. not white
data_panel$white <- NA
data_panel$white[data_panel$race4 == 1] <- 1 #white
data_panel$white[data_panel$race4 != 1] <- 0 #not white

#
data_cross$white <- NA
data_cross$white[data_cross$race4 == 1] <- 1 #white
data_cross$white[data_cross$race4 != 1] <- 0 #not white

# Moderator binary variables ----------------------------------------------

#store wave 0 legitimacy for each individual in panel
ids = unique(data_panel$universal_ID)
data_panel$legit_mod <- NA
for(j in 1: length(unique(data_panel$universal_ID))){
  indiv <- data_panel[data_panel$universal_ID == ids[j],]
  data_panel$legit_mod[data_panel$universal_ID == ids[j] & data_panel$wave == 0] <- data_panel$courtlegit_composite[data_panel$universal_ID == ids[j] & data_panel$wave == 0]
  data_panel$legit_mod[data_panel$universal_ID == ids[j] & data_panel$wave == 1] <- data_panel$courtlegit_composite[data_panel$universal_ID == ids[j] & data_panel$wave == 0]
  data_panel$legit_mod[data_panel$universal_ID == ids[j] & data_panel$wave == 2] <- data_panel$courtlegit_composite[data_panel$universal_ID == ids[j] & data_panel$wave == 0]
}

ids = unique(data_panel$universal_ID)
data_panel$cable_mod <- NA
for(j in 1: length(unique(data_panel$universal_ID))){
  indiv <- data_panel[data_panel$universal_ID == ids[j],]
  data_panel$cable_mod[data_panel$universal_ID == ids[j] & data_panel$wave == 0] <- data_panel$cable[data_panel$universal_ID == ids[j] & data_panel$wave == 0]
  data_panel$cable_mod[data_panel$universal_ID == ids[j] & data_panel$wave == 1] <- data_panel$cable[data_panel$universal_ID == ids[j] & data_panel$wave == 0]
  data_panel$cable_mod[data_panel$universal_ID == ids[j] & data_panel$wave == 2] <- data_panel$cable[data_panel$universal_ID == ids[j] & data_panel$wave == 0]
}

data_panel$cable_mod2 <- NA
data_panel$cable_mod2[data_panel$cable_mod < 5] <- 1 #watches news
data_panel$cable_mod2[data_panel$cable_mod == 5] <- 0 #does not watch news
data_panel$cable_mod2[data_panel$cable_mod > 5] <- NA #watches news

data_cross$cable_mod <- NA
data_cross$cable_mod[data_cross$cable < 5] <- 1 #watches news
data_cross$cable_mod[data_cross$cable == 5] <- 0 #does not watch news
data_cross$cable_mod[data_cross$cable > 5] <- NA #watches news

#store leak and decision cut variables
data_panel <- mutate(data_panel,
                     leak_cut = case_when(
                       wave > 0 ~ 1, #post-leak
                       TRUE ~ 0
                     ))

data_panel <- mutate(data_panel,
                     decision_cut = case_when(
                       wave > 1 ~ 1, #post-decision
                       TRUE ~ 0
                     ))

data_cross <- mutate(data_cross,
                     leak_cut = case_when(
                       wave > 0 ~ 1, #post-leak
                       TRUE ~ 0
                     ))

data_cross <- mutate(data_cross,
                     decision_cut = case_when(
                       wave > 1 ~ 1, #post-decision
                       TRUE ~ 0
                     ))


####### store dv's  #########
dvs3 <- c('personal_roe', 'personal_dobbs', 'courtsize', 'courtterms')

dvs2 <- c('norm', 'moral_norm', 'opinion', 'moral_opinion', 'fetal_scale')

mods <- c('age', 'white', 'educ4', 'pew_religimp', 'pew_bornagain', 'gender')



# regression output tables (panel) --------------------------------------
for(i in 1:length(dvs3)){
legitmod_model <- lm_robust(data_panel[,dvs3[i]] ~ leak_cut*legit_mod + decision_cut*legit_mod, 
          se_type = "HC1", 
          weights= weight_ps,
          fixed_effects = universal_ID,
          data = data_panel)
  
screenreg(legitmod_model, digits = 3, include.ci = TRUE, 
            file = glue("Tables/Heterogeneity_{dvs3[i]}.tex"),
            stars = c(0.001, 0.01, 0.05),
            caption= dvs3[i],
            caption.above = TRUE,
            custom.note = "*$p<.05$.")
}

for(i in 1:length(dvs2)){
  legitmod_model <- lm_robust(data_panel[,dvs2[i]] ~ decision_cut*legit_mod, 
                              se_type = "HC1", 
                              weights= weight_ps,
                              fixed_effects = universal_ID,
                              data = data_panel)
  
  screenreg(legitmod_model, digits = 3, include.ci = TRUE, 
            file = glue("Tables/Heterogeneity_{dvs2[i]}.tex"),
            stars = c(0.001, 0.01, 0.05),
            caption= dvs2[i],
            caption.above = TRUE,
            custom.note = "*$p<.05$.")
}



####cross sectional 
for(i in 1:length(dvs3)){
  legitmod_model <- lm_robust(data_cross[,dvs3[i]] ~ leak_cut*courtlegit_composite + decision_cut*courtlegit_composite + pid, 
                              se_type = "HC1", 
                              weights= weight,
                              data = data_cross)
  
  screenreg(legitmod_model, digits = 3, include.ci = TRUE, 
            file = glue("Tables/Heterogeneity_cross_{dvs3[i]}.tex"),
            stars = c(0.001, 0.01, 0.05),
            caption= dvs3[i],
            caption.above = TRUE,
            custom.note = "*$p<.05$.")
}

#two wave
for(i in 1:length(dvs2)){
  legitmod_model <- lm_robust(data_cross[,dvs2[i]] ~ decision_cut*courtlegit_composite + pid, 
                              se_type = "HC1", 
                              weights= weight,
                              data = data_cross)
  
  screenreg(legitmod_model, digits = 3, include.ci = TRUE, 
            file = glue("Tables/Heterogeneity_cross_{dvs2[i]}.tex"),
            stars = c(0.001, 0.01, 0.05),
            caption= dvs2[i],
            caption.above = TRUE,
            custom.note = "*$p<.05$.")
}

####### moderators ########
for (j in 1:length(dvs3)) {
  for(i in 1:length(mods)){
    model <- lm_robust(data_cross[,dvs3[j]] ~ leak_cut*data_cross[,mods[i]] + decision_cut*data_cross[,mods[i]] + pid, 
                                se_type = "HC1", 
                                weights= weight,
                                data = data_cross)
    
    screenreg(model, digits = 3, include.ci = TRUE, 
              file = glue("Tables/Heterogeneity_cross_{mods[i]}_{dvs3[j]}.tex"),
              stars = c(0.001, 0.01, 0.05),
              caption.above = TRUE,
              custom.note = "*$p<.05$.")
  }
}

for (j in 1:length(dvs2)) {
  for(i in 1:length(mods)){
    model <- lm_robust(data_cross[,dvs2[j]] ~ decision_cut*data_cross[,mods[i]] + pid, 
                       se_type = "HC1", 
                       weights= weight,
                       data = data_cross)
    
    screenreg(model, digits = 3, include.ci = TRUE, 
              file = glue("Tables/Heterogeneity_cross_{mods[i]}_{dvs2[j]}.tex"),
              stars = c(0.001, 0.01, 0.05),
              caption.above = TRUE,
              custom.note = "*$p<.05$.")
  }
}

#panel 
for (j in 1:length(dvs3)) {
  for(i in 1:length(mods)){
    model <- lm_robust(data_panel[,dvs3[j]] ~ leak_cut*data_panel[,mods[i]] + decision_cut*data_panel[,mods[i]], 
                       se_type = "HC1",
                       weights= weight_ps,
                       fixed_effects = universal_ID,
                       data = data_panel)
    
    screenreg(model, digits = 3, include.ci = TRUE, 
              file = glue("Tables/Heterogeneity_panel_{mods[i]}_{dvs3[j]}.tex"),
              stars = c(0.001, 0.01, 0.05),
              caption.above = TRUE,
              custom.note = "*$p<.05$.")
  }
}


for (j in 1:length(dvs2)) {
  for(i in 1:length(mods)){
    model <- lm_robust(data_panel[,dvs2[j]] ~ decision_cut*data_panel[,mods[i]], 
                       se_type = "HC1",
                       weights= weight_ps,
                       fixed_effects = universal_ID,
                       data = data_panel)
    
    screenreg(model, digits = 3, include.ci = TRUE, 
              file = glue("Tables/Heterogeneity_panel_{mods[i]}_{dvs2[j]}.tex"),
              stars = c(0.001, 0.01, 0.05),
              caption.above = TRUE,
              custom.note = "*$p<.05$.")
  }
}

